package com.neusoft.admin.method;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.neusoft.grid.entity.Mission;
import com.neusoft.neps.entity.user;

public class iniRead {
    public static List<Mission> readMissions(String location) throws IOException {
        File file = new File(location);
        if (!file.exists() || file.length() == 0) {
            System.out.println("Data file missing or empty");
            file.createNewFile();
            return new ArrayList<>();
        }
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
            return (List<Mission>) ois.readObject();
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex);
        }
    }

    public static void writeMission(String fileName, List<Mission> missions) {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName/* ,true */))) {
            for (Mission mission : missions) {
                String line = mission.getNumber() + " " + mission.toString() + " " + "未指派";
                bw.write(line);
                bw.newLine();
            }
            System.out.println("mission写入成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void writeListMissions(String location, List<Mission> missions) throws IOException {
        File file = new File(location);
        if (!file.exists() || file.length() == 0) {
            System.out.println("Data file missing or empty");
            file.createNewFile();
        }
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
            oos.writeObject(missions);
        }
    }

    public static Map<String, user> readUsers(String location) throws IOException {
        File file = new File(location);
        Map<String, user> map = new HashMap<>();
        if (!file.exists() || file.length() == 0) {
            System.out.println("Data file missing or empty");
            file.createNewFile();
        }
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
            return (Map<String, user>) ois.readObject();
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex);
        }
    }

    public static void writeUsers(String fileName, Map<String, user> users) {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
            for (user u : users.values()) {
                // 将 admin 对象的属性按逗号分隔组成一行数据i
                String line = "手机号: " + u.getPhoneNumber() + "," + "姓名: " + u.getName() + "," + "性别: " + u.getGender();
                bw.write(line);
                bw.newLine();
            }
            System.out.println("user写入成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
